/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package genetic;

import Utilities.Tradutor;
import java.util.List;
import neural.NeuralNetwork;
import org.neuroph.core.learning.DataSet;
import org.neuroph.util.TransferFunctionType;
import org.uncommons.watchmaker.framework.FitnessEvaluator;

/**
 *
 * @author Celso
 */
class NeuralNetworkEvaluator implements FitnessEvaluator<String> {

    private final Tradutor tradutor;
    private final DataSet trainingSet;
    private final DataSet testingSet;
    Double totalError;
    Double fitness;

    public NeuralNetworkEvaluator(Tradutor tradutor, DataSet trainingSet, DataSet testingSet) {
        this.tradutor = tradutor;
        this.trainingSet = trainingSet;
        this.testingSet = testingSet;
        this.totalError = 0.0;
        this.fitness = 0.0;
    }

    /**
     * Assigns one "fitness point" for every character in the candidate String
     * that matches the corresponding position in the target string.
     */
    @Override
    public double getFitness(String candidate, List<? extends String> population) {

        tradutor.translate(candidate);
        TransferFunctionType transferFunctionType = tradutor.getTransferFunctionType();
        Integer hiddenNeurons = tradutor.getHiddenNeurons();
        Double learnRate = tradutor.getLearnRate();
        Double momentum = tradutor.getMomentum();

        NeuralNetwork nnet = new NeuralNetwork(41, hiddenNeurons, 1, transferFunctionType);
        nnet.setLearningRate(learnRate);
        nnet.setMomentum(momentum);
        nnet.setMaxError(0.01);
        //nnet.setMaxInteraction(5);
        nnet.learn(trainingSet);
        totalError = 1.0 * nnet.test(testingSet);

        fitness = 1 - totalError / testingSet.size();

        return fitness;

    }

    @Override
    public boolean isNatural() {
        return true;
    }

}
